package com.tenchael.iblog.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by tenchael on 25/12/2017.
 */
public class SecurityUtils {

	/**
	 * SHA256摘要
	 *
	 * @param bytes
	 * @return
	 */
	public static byte[] digestWithSha256(byte[] bytes) {
		return DigestUtils.sha256(bytes);
	}

	/**
	 * SHA256摘要,并十六进制编码
	 *
	 * @param string
	 * @return
	 */
	public static String digestHexWithSha256(String string) {
		return MixAll.hexEncode(digestWithSha256(MixAll.getBytesWithUtf8(string)));
	}

	/**
	 * 生成随机字节数组
	 *
	 * @param size
	 * @return
	 */
	public static byte[] genRandom(int size) {
		byte[] bytes = new byte[size];
		ThreadLocalRandom.current().nextBytes(bytes);
		return bytes;
	}
}
